#!/bin/bash
function check_cpu(){
logical_cpu=$(lscpu | grep "^CPU(s):" | awk '{print $2}' | sed 's/[^0-9]//g')
echo "逻辑CPU个数：$logical_cpu"
physical_cpu=$(lscpu | grep "^Socket(s):" | awk '{print $2}' | sed 's/[^0-9]//g')
echo "物理CPU个数：$physical_cpu"
socket_cores=$(lscpu | grep "^Core(s): per socket:" | awk '{print $2}' | sed 's/[^0-9]//g')
echo "每个Socket的核心数：$socket_cores"
core_threads=$(lscpu | grep "^Thread(s): per core:" | awk '{print $2}' | sed 's/[^0-9]//g')
echo "每个核心的线程数：$socket_cores"
}
function check_mem(){
mems_list=$(dmidecode -t memory | grep -i "Volatile Size" | cut -d ":" -f2 | tr -d ' ')
mem_count=0
for mem in $mems_list;do
# 判断内存插槽是否有内存
# echo "内存信息:$mem"
if [ $mem == "None" ];then
continue
else
mem_count=$((mem_count+1))
fi
done
echo "内存条个数：$mem_count"
}
function check_disk(){
# 获取所有磁盘设备名称
disk_devices=$(lsblk -d -o NAME | grep -v '[0-9]$' | tail -n +2)
# 循环检查每个磁盘的健康状态
for device in $disk_devices; do
echo "检查磁盘 $device 的健康状态..."
# 执行磁盘健康状态检查命令
result=$(smartctl -a /dev/$device | grep "overall-health")
disk_health=$(echo $result |cut -d ":" -f2 | tr -d ' ')
# 判断磁盘健康状态
if [ "$disk_health" == "PASSED" ]; then
echo "磁盘 $device 健康状态正常"
fi
if [ "$disk_health" == "FAILED" ]; then
echo "磁盘 $device 健康状态异常"
fi
done
}
function check_network(){
net_count=$(ip link show | grep -c '^[0-9]')
# 去掉lo网卡
nets=$(ip link show | grep '^[0-9]' | awk '{print $2}' | cut -d ":" -f1 | tail -n +2)
nets_state=$(ip link show | grep '^[0-9]' | awk '{print $9}' | cut -d ":" -f1 | tail -n +2)
net_list=()
state_list=()
# 不统计lo网卡
echo "网卡个数为：$(($net_count-1))"
for net in $nets;do
# echo "网卡名称：$net"
net_list+=($net)
done
for state in $nets_state;do
# echo "网卡状态：$state"
state_list+=($state)
done
for ((i=0;i<${#net_list[@]};i++));do
echo "网卡${net_list[i]}的状态为：${state_list[i]}"
done
}
check_cpu
check_mem
check_disk
check_network
